const { Operator } = require('../db');

const outer = () => {};

// 获取操作员列表
outer.getOperators = async () => {
  const operators = await Operator.find({});
  if(!operators) {
    return { 'success': false, 'msg': '操作人员信息查询失败'};
  }

  return { 'success': true, 'msg': '操作人员信息查询成功', data: operators };
};

// 创建操作人员信息

outer.createOperator = async (params) => {
  const { name, workId, age} = params;
  console.log(name);
  // 查询数据是否已经
  let operator = await Operator.findOne({ name, workId });
  if(operator) {
    return { 'success': false, 'msg': '操作员已存在,请勿重复创建'};
  }

  operator = new Operator({
    name,
    age,
    workId,
  });

  await operator.save();
  return { 'success': true, 'msg': '操作员信息创建成功', 'data': operator };
};

module.exports = outer;